<template>
  <div class="code-layout">

    <!-- <div class="code-materials" v-if="detailDataNow">
      <div>
        <plug-state :statusStr="status" v-if="status" />
        <br />
      </div>
      <div class="file">
        <file-list :biz_id="$route.query.id" type="workspace" />
      </div>
    </div> -->

    <div class="code-info">
      <div class="code-info-item">
        <h3 class="info-title">基本信息<i class="el-icon-edit-outline edit" @click="updateBase" /></h3>
        <ul class="info-item">
          <li>
            <span class="key">名称:</span>
            <span class="val">{{ detailData.name }}</span>
          </li>
          <li>
            <span class="key"> 创建人： </span>
            <span class="val">{{
              detailData.create_user && (detailData.create_user.nick_name ||
                detailData.create_user.username)
            }}</span>
          </li>
          <li>
            <span class="key">创建时间： </span>
            <span class="val">{{ detailData.create_time }}</span>
          </li>
        </ul>
      </div>

      <div class="code-info-item">
        <h3 class="info-title">仿真信息 <i class="el-icon-edit-outline edit" @click="updateSimulationEnv" /></h3>
        <ul class="info-item">
          <li>
            <span class="key">仿真名称:</span>
            <span class="val" v-if="detailData.simulation_node_group">{{
              detailData.simulation_node_group.image.name
            }}</span>
          </li>
          <li>
            <span class="key">想定名称:</span>
            <span class="val" v-if="detailData.scenario">{{ detailData.scenario.name }}</span>
          </li>
          <li>
            <span class="key">数量:</span>
            <span class="val" v-if="detailData.simulation_node_group">{{ detailData.simulation_node_group.pod_num
            }}</span>
            <span class="val" v-else>0</span>
          </li>
        </ul>
      </div>

      <div class="code-info-item">
        <h3 class="info-title">开发环境<i class="el-icon-edit-outline edit" @click="updateEnv" /></h3>
        <dl class="code-dl" v-for="(e, i) in detailData.node_group_list" :key="i">
          <ul class="info-item">
            <li>
              <span class="key">节点类型:</span>
              <span class="val">{{ nodeTypeMaps[e.type] }}</span>
            </li>
            <li>
              <span class="key">资源组:</span>
              <span class="val">{{ e.product.name }}</span>
            </li>
            <li>
              <span class="key">镜像:</span>
              <span class="val">{{ e.image.name }}</span>
            </li>
            <li>
              <span class="key">数量:</span>
              <span class="val">{{ e.pod_num }}</span>
            </li>
          </ul>
        </dl>
      </div>

      <div class="code-info-item">
        <h3 class="info-title">节点信息</h3>
        <dl class="code-dl" v-for="(tag, i) in detailData.tcp_port_list" :key="i">
          <ul class="info-item-tcp-port">
            <li>
              <span class="key">协议：{{ tag.proxyProtocol }}</span>
              <span class="key">容器端口：{{ tag.containerPort }}</span>
              <span class="key">映射端口：{{ tag.nodePort }}</span>
              <span class="key">映射地址：{{ tag.endpoint }}</span>
            </li>
          </ul>
        </dl>
      </div>

      <div class="code-info-item">
        <h3 class="info-title">节点容器信息</h3>
        <div class="info-item"><span>仿真节点</span></div>

        <template v-if="detailData.simulation_node_group">
          <dl class="code-dl" v-for="(e, i) in detailData.simulation_node_group.pod_list" :key="i">
            <ul class="info-item-tcp-port">
              <li>
                <span class="key">节点名称：{{ e.name }}</span>
                <span class="key">节点id：{{ e.uid }}</span>
                <span class="key">节点ip：{{ e.pod_ip }}</span>
              </li>
            </ul>
          </dl>
        </template>

        <div class="info-item"><span>业务节点</span></div>

        <template v-if="detailData.node_group_list">
          <dl class="code-dl" v-for="(e, i) in detailData.node_group_list" :key="i">
            <ul class="info-item-tcp-port" v-for="(ite, inx) in e.pod_list" :key="inx">
              <li>
                <span class="key">节点名称：{{ ite.name }}</span>
                <span class="key">节点id：{{ ite.uid }}</span>
                <span class="key">节点ip：{{ ite.pod_ip }}</span>
              </li>
            </ul>
          </dl>
        </template>
      </div>

      <div class="code-md">
        <md-preview :biz_id="$route.query.id" type="workspace" class="preview" />
      </div>
    </div>

    <!-- <el-dialog :visible.sync="showUpdateBaseDialog" title="模型开发基本信息" width="840px">
      <el-form class="form myself-form" ref="form" :rules="baseSubmitRules" :model="taskForm" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input type="text" v-model="taskForm.name" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input type="textarea" :rows="3" v-model="taskForm.desc" />
        </el-form-item>
        <el-form-item label="配图">
          <media-uploader accept=".jpg,.jpeg,.png,.JPG,.JPEG" :src="taskForm.avatar" @avatarPath="avatarPath" />
        </el-form-item>
        <el-form-item label="" prop="tensorboard">
          <el-checkbox v-model="taskForm.tensorboard">tensorboard</el-checkbox>
        </el-form-item>
        <el-form-item label=" ">
          <el-button class="none-type" type="primary" @click="commitBase">确定</el-button>
          <el-button class="none-type" @click="cancelBase">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog> -->

    <!-- <el-dialog :visible.sync="showEnvDialog" title="开发环境基本信息" width="840px">
      <el-form class="form myself-form" ref="envForm" :rules="submitEnvRules" :model="detailDataNow" label-width="120px">
        <div class="node-module">
          <h3 class="node-module-title">主节点</h3>
          <el-form-item label="环境" prop="mainNodeImageId">
            <el-cascader v-model="mainNode.image_group" :options="imageOptions" :show-all-levels="false"
              class="full-extent-select"></el-cascader>
          </el-form-item>
          <el-form-item label="资源组" prop="mainProduct">
            <el-select v-model="mainNode.product_id" prop="main_product" class="full-extent-select">
              <el-option v-for="item in productOptions" :value="item.id" :label="item.name" :key="item.id">
              </el-option>
            </el-select>
          </el-form-item>

          <div class="right-btn-box" v-if="slaveNodeList.length == 0">
            <span class="outin-handle">
              <a href="javascript:;" @click="addSlaveNode()">增加</a>
            </span>
          </div>
        </div>
        <div v-for="(tag, i) in slaveNodeList" :key="i">
          <div class="node-module">
            <h3 class="node-module-title">从节点</h3>
            <el-form-item label="环境" prop="slaveImage">
              <el-cascader v-model="tag.image_group" :options="imageOptions" :show-all-levels="false"
                class="full-extent-select"></el-cascader>
            </el-form-item>
            <el-form-item label="资源组">
              <el-select v-model="tag.product_id" class="full-extent-select">
                <el-option v-for="item in productOptions" :value="item.id" :label="item.name" :key="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="数量">
              <el-input-number v-model="tag.pod_num"></el-input-number>
            </el-form-item>
            <div class="right-btn-box">
              <span class="outin-handle">
                <a href="javascript:;" @click="delSlaveNode(i)">删除</a>
                <a href="javascript:;" @click="addSlaveNode()" v-if="i == slaveNodeList.length - 1">增加</a>
              </span>
            </div>
          </div>
        </div>
        <el-form-item label=" ">
          <el-button class="none-type" type="primary" @click="commitEnv">确定</el-button>
          <el-button class="none-type" @click="cancelEnv">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog> -->

    <!-- <el-dialog :visible.sync="showSimulationDialog" title="仿真信息" width="840px" v-if="detailDataNow.simulation_node_group">
      <el-form class="form myself-form" ref="simulationForm" :model="detailDataNow" label-width="120px"
        :rules="simulationSubmitRules">

        <el-form-item label="仿真环境">
          <el-select v-model="detailDataNow.simulation_node_group.image_id"
            @change="changeSimulationImage(detailDataNow.simulation_node_group.image_id)" prop="simulation_image_id"
            class="full-extent-select">
            <el-option v-for="item in simulationImageOptions" :value="item.id" :label="item.name" :key="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="想定">
          <el-select v-model="detailDataNow.scenario_id" @change="changeScenario" prop="scenario_id"
            class="full-extent-select">
            <el-option v-for="item in scenarioOptions" :value="item.id" :label="item.name" :key="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label=" ">
          <el-button class="none-type" type="primary" @click="commitSimulation">确定</el-button>
          <el-button class="none-type" @click="cancelSimulation">取消</el-button>
        </el-form-item>

      </el-form>
    </el-dialog> -->

    <DialogBox title="模型开发基本信息" :visible="showUpdateBaseDialog" :confirm="commitBase" :close="cancelBase">
      <el-form class="form myself-form" ref="form" :rules="baseSubmitRules" :model="taskForm" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input type="text" v-model="taskForm.name" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input type="textarea" :rows="3" v-model="taskForm.desc" />
        </el-form-item>
        <el-form-item label="配图">
          <media-uploader accept=".jpg,.jpeg,.png,.JPG,.JPEG" :src="taskForm.avatar" @avatarPath="avatarPath" />
        </el-form-item>
        <el-form-item label="" prop="tensorboard">
          <el-checkbox v-model="taskForm.tensorboard">tensorboard</el-checkbox>
        </el-form-item>
      </el-form>
    </DialogBox>

    <DialogBox title="开发环境基本信息" :visible="showEnvDialog" :confirm="commitEnv" :close="cancelEnv">
      <el-form class="form myself-form" ref="envForm" :rules="submitEnvRules" :model="detailDataNow" label-width="120px">
        <div class="node-module">
          <h3 class="node-module-title">主节点</h3>
          <el-form-item label="环境" prop="mainNodeImageId">
            <el-cascader v-model="mainNode.image_group" :options="imageOptions" :show-all-levels="false"
              class="full-extent-select"></el-cascader>
          </el-form-item>
          <el-form-item label="资源组" prop="mainProduct">
            <el-select v-model="mainNode.product_id" prop="main_product" class="full-extent-select">
              <el-option v-for="item in productOptions" :value="item.id" :label="item.name" :key="item.id">
              </el-option>
            </el-select>
          </el-form-item>

          <div class="right-btn-box" v-if="slaveNodeList.length == 0">
            <span class="outin-handle">
              <a href="javascript:;" @click="addSlaveNode()">增加</a>
            </span>
          </div>
        </div>
        <div v-for="(tag, i) in slaveNodeList" :key="i">
          <div class="node-module">
            <h3 class="node-module-title">从节点</h3>
            <el-form-item label="环境" prop="slaveImage">
              <el-cascader v-model="tag.image_group" :options="imageOptions" :show-all-levels="false"
                class="full-extent-select"></el-cascader>
            </el-form-item>
            <el-form-item label="资源组">
              <el-select v-model="tag.product_id" class="full-extent-select">
                <el-option v-for="item in productOptions" :value="item.id" :label="item.name" :key="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="数量">
              <el-input-number v-model="tag.pod_num"></el-input-number>
            </el-form-item>
            <div class="right-btn-box">
              <span class="outin-handle">
                <a href="javascript:;" @click="delSlaveNode(i)">删除</a>
                <a href="javascript:;" @click="addSlaveNode()" v-if="i == slaveNodeList.length - 1">增加</a>
              </span>
            </div>
          </div>
        </div>
      </el-form>
    </DialogBox>

    <DialogBox title="仿真信息" :visible="showSimulationDialog" :confirm="commitSimulation" :close="cancelSimulation"
      v-if="detailDataNow.simulation_node_group">
      <el-form class="form myself-form" ref="simulationForm" :model="detailDataNow" label-width="120px"
        :rules="simulationSubmitRules">

        <el-form-item label="仿真环境">
          <el-select v-model="detailDataNow.simulation_node_group.image_id"
            @change="changeSimulationImage(detailDataNow.simulation_node_group.image_id)" prop="simulation_image_id"
            class="full-extent-select">
            <el-option v-for="item in simulationImageOptions" :value="item.id" :label="item.name" :key="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="想定">
          <el-select v-model="detailDataNow.scenario_id" @change="changeScenario" prop="scenario_id"
            class="full-extent-select">
            <el-option v-for="item in scenarioOptions" :value="item.id" :label="item.name" :key="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="数量">
          <el-input-number v-model="detailDataNow.simulation_node_group.pod_num"></el-input-number>
        </el-form-item>
      </el-form>
    </DialogBox>
  </div>
</template>

<script>
import DialogBox from '@/components/module/dialog-box.vue';
import { getGameExperimentDetail, putGameExperiment } from '@/api/gameExperiment'
// import plugState from '@/components/module/plug-state'
import { getAllImageGroups, getAllImages, getImageDetail } from '@/api/image'
import { getAllScenarios } from '@/api/scenario'
import { mainImageRule, mainProductRule, slaveImageRule } from '@/utils/rule'
import { getAllProducts } from '@/api/product'
export default {
  data() {
    return {
      status: null,
      mainNode: null,
      detailDataNow: null,
      slaveNodeList: [],
      productOptions: [],
      scenarioOptions: [],
      simulationImageOptions: [],
      showSimulationDialog: false,
      showEnvDialog: false,
      addForm: {},
      imageOptions: [],
      simulationSubmitRules: {

      },
      submitEnvRules: {
        mainNodeImageId: [
          { required: true, validator: mainImageRule.bind(this, this), trigger: 'change' }
        ],
        mainProduct: [
          { required: true, validator: mainProductRule.bind(this, this), trigger: 'change' }
        ],
        tagImageId: [
          { required: true, validator: slaveImageRule.bind(this, this), trigger: 'change' }
        ],

      },
      showUpdateBaseDialog: false,
      baseSubmitRules: {
        name: [
          { required: true, message: "该项为必填项" },
          { required: true, min: 1, max: 40, message: "长度在1到40个字符" },
        ],
        desc: [
          { required: true, message: "该项为必填项" },
          { required: true, min: 1, max: 200, message: "长度在1到40个字符" },
        ],
      },
      taskForm: {},
    };
  },

  props: ['detailData'],
  watch: {
    detailData(val) {
      this.status = val.status;
      this.detailDataNow = JSON.parse(JSON.stringify(val))
      let mainNodeList = this.detailDataNow.node_group_list.filter(node => node.type == 1);
      this.mainNode = mainNodeList[0]
      this.slaveNodeList = this.detailDataNow.node_group_list.filter(node => node.type != 1);
      this.getAddForm(this.detailDataNow)
      console.log("detailData", this.detailData)
    }
  },
  components: {
    // plugState,
    DialogBox,
  },
  async mounted() {
    this.$forceUpdate();
  },
  created() {
    this.detailDataNow = JSON.parse(JSON.stringify(this.detailData))
    this.status = this.detailData.status;
    console.log("status", this.status)
    let mainNodeList = this.detailDataNow.node_group_list.filter(node => node.type == 1);
    this.mainNode = mainNodeList[0]
    this.slaveNodeList = this.detailDataNow.node_group_list.filter(node => node.type != 1);
    console.log("dataData", this.detailData)
    this.getAllImageGroups()
    this.getSimulationImage()
    if (this.detailDataNow.simulation_node_group) {
      this.changeSimulationImage(this.detailDataNow.simulation_node_group.image_id)
    }
    this.getAddForm(this.detailDataNow)
    this.getProductList()
  },
  destroyed() {

  },
  methods: {
    avatarPath(data) {
      this.taskForm.avatar = data;
    },
    updateBase() {
      this.taskForm = this.detailDataNow
      this.showUpdateBaseDialog = true
    },

    commitBase() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let res = await putGameExperiment(this.taskForm);
          if (res && res.data) {
            this.$emit("infoData")
            this.showUpdateBaseDialog = false
          }
        }
      })
    },
    cancelBase() {
      this.showUpdateBaseDialog = false
    },

    async setAddForm() {
      console.log('this.simulation', this.simulation)
      this.addForm.node_group_list = []
      this.mainNode.type = 1
      this.mainNode.image_id = this.mainNode.image_group[2]
      this.addForm.node_group_list.push(this.mainNode)
      for (let i = 0; i < this.slaveNodeList.length; i++) {
        this.slaveNodeList[i].type = 2
        this.slaveNodeList[i].image_id = this.slaveNodeList[i].image_group[2]
        this.addForm.node_group_list.push(this.slaveNodeList[i])
      }
      console.log('addForm', this.addForm)
    },
    async getAddForm(data) {
      this.addForm = data
      this.port_list = []
      this.slaveNodeList = []
      for (let node of data.node_group_list) {
        if (node.type == 1) {
          this.mainNode = node
          let res = await getImageDetail({ id: this.mainNode.image_id })
          let mainImage = res.data
          this.mainNode.image_group = [mainImage.visibility.toString(), mainImage.engine.toString(), mainImage.id]
        } else {
          let res = await getImageDetail({ id: node.image_id })
          let slaveImage = res.data
          let slaveNode = node
          slaveNode.image_group = [slaveImage.visibility.toString(), slaveImage.engine.toString(), slaveImage.id]
          this.slaveNodeList.push(slaveNode)
        }
        console.log("slaveNodeList", this.slaveNodeList.length)
      }
    },
    updateEnv() {
      this.showEnvDialog = true
    },
    commitEnv() {
      this.setAddForm()
      this.$refs.envForm.validate(async (valid) => {
        if (valid) {
          let res = await putGameExperiment(this.detailDataNow);
          if (res && res.data) {
            this.showEnvDialog = false
            this.freshData()
          }
        }
      })
    },
    cancelEnv() {
      this.showEnvDialog = false
    },
    addSlaveNode() {
      let data = { type: 1, pod_num: 1 }
      this.slaveNodeList.push(data)
    },
    delSlaveNode(i) {
      this.slaveNodeList.splice(i, 1)
    },
    async getAllImageGroups() {
      let res = await getAllImageGroups()
      this.imageOptions = res.data
    },
    updateSimulationEnv() {
      this.showSimulationDialog = true
    },
    async commitSimulation() {
      let res = await putGameExperiment(this.detailDataNow);
      if (res && res.data) {
        this.showSimulationDialog = false
        this.freshData()
      }
    },
    cancelSimulation() {
      this.showSimulationDialog = false
    },
    async getProductList() {
      let res = await getAllProducts()
      this.productOptions = res.data
    },
    async getSimulationImage() {
      let ret = await getAllImages({ visibility: '1', type: 3 })
      this.simulationImageOptions = ret.data
    },
    async changeSimulationImage(simulation_id) {
      console.log("changeSimulationImage")
      let ret = await getAllScenarios({ imageId: simulation_id })
      console.log('ret', ret.data)
      this.scenarioOptions = ret.data

    },
    changeScenario() {
      this.$forceUpdate()
    },
    async freshData() {
      console.log("detailData")
      let res = await getGameExperimentDetail({ id: this.detailData.id });
      if (res && res.data) {
        console.log("emit")
        this.$emit('update:detailData', res.data) //通过$emit触发父组件
      }
    },
    editState(state) {
      console.log(state)
    }
  }
}
</script>
<style lang="less" scoped>
@import "~@/assets/css/code.less";
</style>
